package com.yuanren.em.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanren.em.annotation.Authority;
import com.yuanren.em.common.Result;
import com.yuanren.em.entity.AuthorityType;
import com.yuanren.em.entity.Comment;
import com.yuanren.em.mapper.CommentMapper;
import com.yuanren.em.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Authority(AuthorityType.requireLogin)
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    
    @Resource
    private CommentService commentService;
    
    /**
     * 创建评论
     */
    @PostMapping
    public Result createComment(@RequestBody Comment comment) {
        try {
            commentService.createComment(comment);
            return Result.success("评论发表成功");
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }
    
    /**
     * 根据商品ID获取评论列表
     */
    @GetMapping("/good/{goodId}")
    public Result getCommentsByGoodId(@PathVariable Long goodId) {
        List<Comment> comments = commentService.getCommentsByGoodId(goodId);
        return Result.success(comments);
    }
    
    /**
     * 获取当前用户的评论列表
     */
    @GetMapping("/my")
    public Result getCurrentUserComments() {
        List<Comment> comments = commentService.getCurrentUserComments();
        return Result.success(comments);
    }
    
    /**
     * 检查当前用户是否已经评论过某个商品
     */
    @GetMapping("/check/{goodId}/{orderId}")
    public Result checkCommented(@PathVariable Long goodId, @PathVariable Long orderId) {
        boolean hasCommented = commentService.hasCurrentUserCommented(goodId, orderId);
        return Result.success(hasCommented);
    }
    
    /**
     * 获取商品评论统计信息
     */
    @GetMapping("/statistics/{goodId}")
    public Result getCommentStatistics(@PathVariable Long goodId) {
        CommentMapper.CommentStatistics statistics = commentService.getCommentStatistics(goodId);
        return Result.success(statistics);
    }
    
    /**
     * 商家回复评论（需要管理员权限）
     */
    @Authority(AuthorityType.requireAuthority)
    @PostMapping("/reply/{commentId}")
    public Result replyComment(@PathVariable Long commentId, @RequestBody Map<String, String> request) {
        try {
            String replyContent = request.get("replyContent");
            commentService.replyComment(commentId, replyContent);
            return Result.success("回复成功");
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }
    
    /**
     * 删除评论（需要管理员权限）
     */
    @Authority(AuthorityType.requireAuthority)
    @DeleteMapping("/{commentId}")
    public Result deleteComment(@PathVariable Long commentId) {
        try {
            commentService.deleteComment(commentId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }
    
    /**
     * 分页获取所有评论（管理员用）
     */
    @Authority(AuthorityType.requireAuthority)
    @GetMapping("/page")
    public Result getCommentsPage(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize,
                                  @RequestParam(required = false) String goodName,
                                  @RequestParam(required = false) String userName,
                                  @RequestParam(required = false) Integer rating) {
        try {
            IPage<Comment> result = commentService.getCommentsPageWithInfo(pageNum, pageSize, goodName, userName, rating);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "获取评论列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取所有评论（管理员用）
     */
    @Authority(AuthorityType.requireAuthority)
    @GetMapping("/all")
    public Result getAllComments() {
        List<Comment> comments = commentService.list();
        return Result.success(comments);
    }
} 